﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CongregacaoOs.Data;
using CongregacaoOs.Business;

namespace CongregacaoOs.WebForm.Cadastros
{
    public partial class Estado : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["DadosGerais"] == null) Response.Redirect("~/Cadastros/Account/Login.aspx");

            if (Session["TipoUsuario"].ToString() == "Local") Response.Redirect("~/Cadastros/Home.aspx");

            Page.Form.DefaultButton = btnBuscar.UniqueID;

            if (!IsPostBack)
            {
                AtualizarGridView();
                AtualizarDropPais();
                AtualizarDropPaisFiltro();

            }
        }

        public void AtualizarGridView()
        {
            ccEstadoBus bus = new ccEstadoBus();
            ccEstado ent = new ccEstado();

            ent.nome = txtBuscarNome.Text;
            ent.sigla = "";
            ent.idPais = dropPaisFiltro.SelectedValue != "" ? Convert.ToInt32(dropPaisFiltro.SelectedValue) : 0;
            gvEstado.DataSource = bus.EstadoSelect(ent);
            gvEstado.DataBind();
        }


        public void AtualizarDropPais()
        {
            ccPaisBus bus = new ccPaisBus();
            ccPais ent = new ccPais();
            dropPais.DataSource = bus.PaisSelect(ent);
            dropPais.DataBind();
        }

        public void AtualizarDropPaisFiltro(int aID = 0)
        {
            using (ccPaisBus bus = new ccPaisBus())
            {
                ccPais ent = new ccPais();
                ent.idPais = aID;
                dropPaisFiltro.DataSource = bus.PaisSelect(ent);
                dropPaisFiltro.DataBind();
            }


        }

        public void LimparCampos(Control controle)
        {
            foreach (Control ctrl in controle.Controls)
            {
                if (ctrl is TextBox) ((TextBox)ctrl).Text = string.Empty;
                else if (ctrl is DropDownList) ((DropDownList)ctrl).SelectedIndex = 0;
                else if (ctrl.Controls.Count > 0)
                {
                    LimparCampos(ctrl);
                }
            }
        }


        protected void gvEstado_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            AtualizarGridView();
            gvEstado.PageIndex = e.NewPageIndex;
            gvEstado.DataBind();
        }

        protected void gvEstado_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EDITAR" || e.CommandName == "EXCLUIR")
            {
                GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);

                int index = row.RowIndex;

                hdID.Value = gvEstado.Rows[index].Cells[1].Text.Trim();
                HiddenField hdIdPais = (gvEstado.Rows[index].FindControl("hdIdPais") as HiddenField);

                if (e.CommandName == "EDITAR")
                {
                    hfIndexAccordion.Value = "1";
                    AtualizarDropPais();

                    txtNome.Text = gvEstado.Rows[index].Cells[2].Text.Trim();
                    dropPais.SelectedValue = hdIdPais.Value;
                    txtSigla.Text = gvEstado.Rows[index].Cells[3].Text.Trim();
                }
                else if (e.CommandName == "EXCLUIR")
                {
                    ccEstadoBus bus = new ccEstadoBus();
                    bus.EstadoDelete(Convert.ToInt32(hdID.Value));
                }
            }
            AtualizarGridView();
        }

        protected void gvEstado_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgEditar = (e.Row.FindControl("imgEditar") as ImageButton);
                imgEditar.CommandArgument = e.Row.RowIndex.ToString();

                ImageButton imgDelete = (e.Row.FindControl("imgDelete") as ImageButton);
                imgDelete.CommandArgument = e.Row.RowIndex.ToString();

                imgDelete.Attributes.Add("onclick", "javascript:return " +
                        "confirm('Confirmar exclusão do registro: " +
                        DataBinder.Eval(e.Row.DataItem, "estado") + "?')");
            }
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            try
            {
                ccEstadoBus bus = new ccEstadoBus();
                ccEstado ent = new ccEstado();

                ent.nome = txtNome.Text.Trim();
                ent.idPais = Convert.ToInt32(dropPais.SelectedValue);
                ent.sigla = txtSigla.Text.Trim();
                if (hdID.Value != "")
                    ent.idEstado = Convert.ToInt32(hdID.Value);

                bus.EstadoSaveOrUpdate(ent);
                hfIndexAccordion.Value = "0";
                AtualizarGridView();
                Functions.LimparCampos(this);

                MessageWindow.MessageType = true;
                MessageWindow.Texto = "Registro salvo com sucesso!!!";
                MessageWindow.ShowWindow();
            }
            catch (Exception)
            {

                MessageWindow.MessageType = false;
                MessageWindow.Texto = "Houve um erro ao salvar o registro. Tente novamente ou contate o suporte!!!";
                MessageWindow.ShowWindow();
            }

        }

        protected void btnVoltar_Click(object sender, EventArgs e)
        {
            hfIndexAccordion.Value = "0";
            hdID.Value = "";
            Functions.LimparCampos(this);
        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        {
            AtualizarGridView();
        }

        protected void btnAdicionarNovo_Click(object sender, EventArgs e)
        {
            hfIndexAccordion.Value = "1";
            hdID.Value = "";
            LimparCampos(this);
        }

        protected void dropPais_DataBound(object sender, EventArgs e)
        {
            dropPais.Items.Insert(0, new ListItem("Escolha o País...", String.Empty));
        }

        protected void dropPaisFiltro_DataBound(object sender, EventArgs e)
        {
            dropPaisFiltro.Items.Insert(0, new ListItem("Escolha o País...", String.Empty));
        }

        protected void dropPaisFiltro_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarGridView();
        }



    }
}